﻿using System.Collections.Generic;
using System.Linq;
using Domain.Infrastructure;

namespace Application.Infrastructure
{
    /// <summary>
    ///具有（创建、更新、删除）约束的 服务基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseService_C_M_D<T> : BaseService<T> where T : class, ICreationAudited, IModifyAudited, IDeleteAudited, new()
    {
        public BaseService_C_M_D(ILog log, IRepositoryBase<T> repository, string userId) : base(log, repository, userId)
        {
        }
        /// <summary>
        /// 写入实体数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <returns></returns>
        public override bool Insert(T entity)
        {
            entity.Create(UserId);
            entity.DeleteMark = false;
            return Repository.Insert(entity);
        }
        /// <summary>
        /// 更新实体数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public override bool Update(T entity)
        {
            entity.Modify(UserId, ((IModifyAudited)entity).Id);
            return Repository.Update(entity);
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <returns></returns>
        public override bool Delete(T entity)
        {
            entity.Remove(UserId);
            return Repository.Update(entity);
        }
        public override List<T> GetList(Pagination pagination)
        {
            var model = Repository.GetList().Where(t => t.DeleteMark == false).ToList();
            return PagingHelper<T>.Query(model, pagination);
        }

        public override List<T> GetList()
        {
            return Repository.GetList().Where(t => t.DeleteMark == false).ToList();
        }
        public override bool Delete(string id)
        {
            T model = new T();
            ((ICreationAudited)model).Id = id;
            model.Remove(UserId);
            return Delete(model);
        }



    }
}
